package com.shz.appletsapi.service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.shz.appletsapi.common.util.DateUtils;
import com.shz.appletsapi.model.Miner;
import com.shz.appletsapi.model.po.MineAccount;
import com.shz.appletsapi.model.po.MineConfig;
import com.shz.appletsapi.model.po.MineInfo;
import com.shz.appletsapi.model.po.MineStatistics;
import com.shz.appletsapi.prop.ShzDefaults;
import com.shz.appletsapi.repository.MineAccountRepository;
import com.shz.appletsapi.repository.MineConfigRepository;
import com.shz.appletsapi.repository.MineInfoRepository;
import com.shz.appletsapi.repository.MineStatisticsRepository;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MineInfoService {
	
	@Autowired
	private MineInfoRepository mineInfoRepository;
	
	@Autowired
	private MineStatisticsRepository mineStatisticsRepository;
	
	@Autowired
	private MineAccountService mineAccountService;
	
	@Autowired
	private MineConfigRepository mineConfigRepository;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@SuppressWarnings("rawtypes")
	@Autowired
	private RedisTemplate redisTemplate;
	
	public MineInfo findByTsid(String tsid) {
		return mineInfoRepository.findByTsid(tsid);
	}
	
	
	@Async
	public void saveMineInfo(Miner miner) {
		MineInfo mineInfo=findByTsid(miner.getTsid());
		if(mineInfo ==null) {
			mineInfo = new MineInfo();
			mineInfo.setTsid(miner.getTsid());
			mineInfo.setMachine_name(miner.getChannel()+"_机器_"+miner.getMac());
		}		
		mineInfo.setOs_version(miner.getOs_version());
		mineInfo.setIp_addr(miner.getIp_addr());
		mineInfo.setGraph_type(miner.getGraph_type());
		mineInfo.setCpu_model(miner.getCpu_model());
		mineInfo.setDisk_storage(miner.getDisk_storage());
		mineInfo.setGraph_model(miner.getGraph_model());
		mineInfo.setGraph_storage(miner.getGraph_storage());
		mineInfo.setGraph_bit(miner.getGraph_bit());
		mineInfo.setGraph_hz(miner.getGraph_hz());
		mineInfo.setChannel(miner.getChannel());
		mineInfo.setMac(miner.getMac());
		mineInfo.setCreate_time(new Date());
		mineInfoRepository.save(mineInfo);
		log.info("保存设备信息成功");
	}
	
	
	public Integer getMaxMacValue(String channel) {
		String sql="SELECT MAX(machine_value) value  FROM mine_info WHERE channel =?";
		List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,new Object[] {channel});
		if(list!=null && !list.isEmpty()) {
			return (Integer) list.get(0).get("value");
		}
		return 0;
	}
	
	
	
	
	
	
	
	
	public void mineStatistics() {
		List<MineInfo> list = (List<MineInfo>) mineInfoRepository.findAll();
		String data=DateUtils.format(DateUtils.setDays(new Date(), -1), DateUtils.PATTERN_DATE);
		Map<String,String> resultMap =new HashMap<>();
		String cacheKey="";		
		
		log.info(data);
		for(MineInfo mineInfo : list) {
			MineStatistics mineStatistics =new MineStatistics();
			cacheKey = mineInfo.getTsid()+"_"+data;
			mineStatistics.setDiff("0");
			mineStatistics.setOnlineTime("0");
			mineStatistics.setIncome(new BigDecimal(0.00));
			 if(redisTemplate.hasKey(cacheKey)) {
				 resultMap= redisTemplate.opsForHash().entries(cacheKey);  
				 String nowcount=resultMap.get(ShzDefaults.DEFALUTS_NOWCOUNT);
				 String diffs=resultMap.get(ShzDefaults.DEFALUTS_DIFF);
				 mineStatistics.setOnlineTime(String.valueOf((Integer.valueOf(nowcount) * 5)));
				 mineStatistics.setDiff(jisuandiff(nowcount,diffs));
				 mineStatistics.setIncome(CalculationIncome(mineInfo.getChannel(),(Integer.valueOf(nowcount) * 5),jisuandiff(nowcount,diffs)));
			 }
			 mineStatistics.setTsid(mineInfo.getTsid());
			 mineStatistics.setChannel(mineInfo.getChannel());
			 mineStatistics.setCreateDate(DateUtils.setDays(new Date(), -1));
			 mineStatistics.setCreateTime(new Date());			 
			 mineStatisticsRepository.save(mineStatistics);
		}
		log.info("task in success");
	}
	
	
	public String jisuandiff(String str1,String str2) {
		DecimalFormat    df   = new DecimalFormat("######0.00");  
	Double aa=	Double.valueOf(str2) / Double.valueOf(str1) / 1000;
	return df.format(aa);
	}
	
	
	public BigDecimal CalculationIncome(String channel,Integer mineMinutes,String diffs) {
		List<MineAccount> accounts= mineAccountService.findByStatus(1,channel);
		if(accounts == null || accounts.isEmpty()) {
			return null;
		}
		MineAccount mineAccount=accounts.get(0);
		String coinName =mineAccount.getCoin_name();
		String mineNum;
		MineConfig config =null;
		config=mineConfigRepository.findByKey(coinName+ShzDefaults.DEFALUTS_MINENUM);
		mineNum=config.getValue();
		BigDecimal seconds= new BigDecimal(mineMinutes*60);
		BigDecimal sumDiffs= seconds.multiply(new BigDecimal(diffs));
		BigDecimal sumIncome=sumDiffs.divide(new BigDecimal(mineNum),2,BigDecimal.ROUND_HALF_UP);
		log.info(sumIncome.toString());
		return sumIncome;
	}
	
	
	

}
